{% extends "_layouts/edit.html" -%}
{% block body %}
{{ form::input(name="name", title="角色名称", value=row.name) }}
{{ form::remark(value=row.remark) }}
{% set ids = "," ~ row.menu_ids ~ "," -%}
<div class="layui-form-item">
    <label for="menus" class="layui-form-label"> <span class="x-red">*</span>菜单权限: </label>
    <div class="layui-input-inline" style="width: 380px;">
        {% for menu in menus -%}
        <fieldset class="layui-elem-field" style="background-color: #fff">
            <legend style="font-size: 13px;">{{menu.name}}</legend>
            <div class="layui-field-box"> 
                {% for sub in menu.menus -%}
                {% set my_id = "," ~ sub.id ~ "," %}
                <input type="checkbox" class="sub_menu" name="sub_menu_{{sub.id}}" title="{{sub.name}}" lay-skin="primary" value="{{sub.id}}" {% if ids is containing(my_id) %}checked="checked"{% endif %} />
                {% endfor -%}
            </div>
        </fieldset>
        {% endfor -%}
    </div>
    <input type="hidden" value="{{row.menu_ids}}" name="menu_ids" id="menu_ids" />
    <div class="layui-form-mid layui-word-aux"> </div>
</div>
{{ form::seq(value=row.seq) }}
<script>
layui.use(['jquery', 'form'], function() { 
    var $ = layui.jquery, form = layui.form;
    form.on('checkbox', function(obj) { 
        var menu_ids = new Array();
        $(".sub_menu").each(function() { 
            var that = $(this);
            var checked = that.is(":checked");
            if (checked) { 
                menu_ids.push(that.val());
            }
        });
        $("#menu_ids").val(menu_ids.toString());
    });
});
layui.validators = { 
    name: function(value) { 
        if (value.length < 2 || value.length > 15) { 
            return '角色名称长度必须在2-15之间';
        }
    },
    remark: function(value) { 
        if (value.length > 100) { 
            return '备注长度不能超过100';
        }
    }
};
</script>
{% endblock body -%}
